﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;

namespace BLL
{
    public class CommentBLL:ICommentBLL
    {
        //传入图片id和commentText设置评论
        public bool commentPhoto(int userIdFrom, int userIdTo, int photoId, string commentText) 
        {
            bool validator = false;
            DAL.CommentDAL commentDAL = (DAL.CommentDAL)DALFAC.DataAccess.CreateComment();
            if (null != commentDAL)
            {
                Model.Comment commentModel = new Model.Comment();
                commentModel.setCM_userIdFrom(userIdFrom);
                commentModel.setCM_userIdTo(userIdTo);
                commentModel.setCM_photoId(photoId);
                commentModel.setCM_time(DateTime.Now);
                commentModel.setCM_text(commentText);
                

                if (commentDAL.addComment(commentModel))
                {
                    DAL.PhotoDAL photoDal = (DAL.PhotoDAL)DALFAC.DataAccess.CreatePhoto();
                    if (null != photoDal)
                    {
                        Model.Photo photoModel = (Model.Photo)photoDal.getPhotoById(photoId);
                        int count = photoModel.getPT_commentCount() + 1;
                        photoModel.setPT_commentCount(count);
                        validator = photoDal.Update(photoModel);
                    }
                }
                
            }

            return validator;
        }


        //传入图片id获取是否存在评论
        public bool commentExist(int photoId)
        {
            bool validator = false;
            DAL.CommentDAL commentDAL = (DAL.CommentDAL)DALFAC.DataAccess.CreateComment();
            if (null != commentDAL)
            {
                validator = commentDAL.commentExist(photoId);
            }

            return validator;
        }


        //删除给定评论
        public bool deleteComment(int commentId)
        {
            bool validator = false;
            DAL.CommentDAL commentDAL = (DAL.CommentDAL)DALFAC.DataAccess.CreateComment();
            if (null != commentDAL)
            {
                Model.Comment commentModel = commentDAL.getModel(commentId);
                int photoId = commentModel.getCM_photoId();


                if (commentDAL.deleteCommentById(commentId))
                {
                    DAL.PhotoDAL photoDal = (DAL.PhotoDAL)DALFAC.DataAccess.CreatePhoto();
                    if (null != photoDal)
                    {
                        Model.Photo photoModel = (Model.Photo)photoDal.getPhotoById(photoId);
                        int count = photoModel.getPT_commentCount() - 1;
                        if (count >= 0)
                        {
                            photoModel.setPT_commentCount(count);
                            validator = photoDal.Update(photoModel);
                        }
                    }
                }
            }

            return validator;
        }


        //删除所有评论
        public bool deleteAllComment(int photoId)
        {
            bool validator = false;
            DAL.CommentDAL commentDAL = (DAL.CommentDAL)DALFAC.DataAccess.CreateComment();
            if (null != commentDAL)
            {
                validator = commentDAL.deleteAllByPhotoId(photoId);

                if (commentDAL.deleteAllByPhotoId(photoId))
                {
                    DAL.PhotoDAL photoDal = (DAL.PhotoDAL)DALFAC.DataAccess.CreatePhoto();
                    if (null != photoDal)
                    {
                        Model.Photo photoModel = (Model.Photo)photoDal.getPhotoById(photoId);                    
                            photoModel.setPT_commentCount(0);
                            validator = photoDal.Update(photoModel);                        
                    }
                }
            }

            return validator;
        }


        //获取所有评论
        public List<Model.Comment> getAllComment(int photoId)
        {
            List<Model.Comment> list = new List<Model.Comment>();
            DAL.CommentDAL commentDAL = (DAL.CommentDAL)DALFAC.DataAccess.CreateComment();
            if (null != commentDAL)
            {
                list = commentDAL.getModelByPhotoId(photoId);
            }

            return list;
        }
    }
}
